package com.dgut.medicalsystem.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.medicalsystem.entity.Notice;
import com.dgut.medicalsystem.entity.dto.Pagination;
import com.dgut.medicalsystem.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 坚宝医疗
 * @since 2021-09-03
 */

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @SaCheckLogin
    @RequestMapping("checkNotices")
    public IPage<Notice> checkNotices(@RequestBody Pagination pagination) {
        try {
            Page<Notice> page = new Page<>(pagination.getPage(), pagination.getSize());
            int userid = Integer.parseInt(StpUtil.getLoginId().toString().substring(1));
            return noticeService.page(page, new QueryWrapper<Notice>().eq("owner", userid));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @SaCheckLogin
    @RequestMapping("/getUnread")
    public Map<String,Object> getUnread(){
        Map<String,Object> map = new HashMap<>();
        try{
            int userid = Integer.parseInt(StpUtil.getLoginId().toString().substring(1));
            List<Notice> list = noticeService.getByStatus(userid,0);
            map.put("ret",0);
            map.put("data",list);
            map.put("msg","获取成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("ret",1);
            map.put("data",null);
            map.put("msg","获取失败");
        }
        return map;
    }

    @SaCheckLogin
    @RequestMapping("/getRead")
    public Map<String,Object> getRead(){
        Map<String,Object> map = new HashMap<>();
        try{
            int userid = Integer.parseInt(StpUtil.getLoginId().toString().substring(1));
            List<Notice> list = noticeService.getByStatus(userid,1);
            map.put("ret",0);
            map.put("data",list);
            map.put("msg","获取成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("ret",1);
            map.put("data",null);
            map.put("msg","获取失败");
        }
        return map;
    }

    @SaCheckLogin
    @RequestMapping("/getDelete")
    public Map<String,Object> getDelete(){
        Map<String,Object> map = new HashMap<>();
        try{
            int userid = Integer.parseInt(StpUtil.getLoginId().toString().substring(1));
            List<Notice> list = noticeService.getByStatus(userid,-1);
            map.put("ret",0);
            map.put("data",list);
            map.put("msg","获取成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("ret",1);
            map.put("data",null);
            map.put("msg","获取失败");
        }
        return map;
    }

    @SaCheckLogin
    @RequestMapping("/setRead")
    public Map<String,Object> setRead(@RequestBody Notice notice){
        Map<String,Object> map = new HashMap<>();
        try{
            //int userid = Integer.parseInt(StpUtil.getLoginId().toString().substring(1));
            boolean flag = noticeService.setStatus(notice,1);
            if (flag){
                map.put("ret",0);
                map.put("msg","修改成功");
            }else{
                map.put("ret",2);
                map.put("data",null);
                map.put("msg","修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put("ret",1);
            map.put("data",null);
            map.put("msg","修改错误");
        }
        return map;
    }

    @SaCheckLogin
    @RequestMapping("deleteInfo")
    public Map<String,Object> deleteInfo(@RequestBody Notice notice){
        Map<String,Object> map = new HashMap<>();
       try {
           boolean flag = noticeService.setStatus(notice,-1);
           if (flag){
               map.put("ret",0);
               map.put("msg","删除成功");
           }else{
               map.put("ret",2);
               map.put("data",null);
               map.put("msg","删除失败");
           }
       }catch (Exception e){
           e.printStackTrace();
           map.put("ret",1);
           map.put("data",null);
           map.put("msg","删除错误");
       }
        return map;
    }


}
